using System;
using System.Collections.Generic;
using System.Text;
using AntEater.ComponentModel.BuildElements;
using System.Xml.XPath;
using System.Reflection;
using System.Xml;
using AntEater.ComponentModel;
using System.Diagnostics.CodeAnalysis;

namespace AntEater.Runtime.Services.Transformation
{

    /// <summary>
    /// Provides a transfomer for the ProjectBuildElement.
    /// </summary>
    [BuildElementTransformer(typeof(ProjectBuildElement), XPathNodeType.Element)]
    internal class ProjectTransformer:BuildElementTransformerBase
    {

        /// <summary>
        /// Initializes a new instance of the <see cref="T:ProjectTransformer"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        public ProjectTransformer(object context)
            : base(context) {
        }

        /// <summary>
        /// Transforms an <c>BuildElement</c> into XML.
        /// </summary>
        /// <param name="buildElement">The build element.</param>
        /// <param name="writer">The writer.</param>
        /// <param name="errors">The errors.</param>
        public override void GetXml(IBuildElement buildElement, XmlWriter writer, List<TransformationError> errors) {
            ProjectBuildElement element = buildElement as ProjectBuildElement;

            if (element != null) {

                BuildElementAttribute buildElementAttribute
                    = ComponentModel.Utilities.GetTypeAttributeFromObject<BuildElementAttribute>(buildElement);

                if (buildElementAttribute != null) {

                    if (string.IsNullOrEmpty(element.XmlNamespace)) {
                        errors.Add(new TransformationError("Xml Namespace is not defined!", element));
                    }

                    writer.WriteStartElement(buildElementAttribute.Name,element.XmlNamespace);

                    string initialTargets = BuildElementPropertyTransformationHelper.ListOfTargetsToXml(element.InitialTargets);
                    if (!string.IsNullOrEmpty(initialTargets)){
                        writer.WriteAttributeString(this.GetBuildElementPropertyName("InitialTargets"), initialTargets);
                    }
                                        
                    string defaultTargets = BuildElementPropertyTransformationHelper.ListOfTargetsToXml(element.DefaultTargets);
                    if (!string.IsNullOrEmpty(defaultTargets)) {
                        writer.WriteAttributeString(this.GetBuildElementPropertyName("DefaultTargets"), defaultTargets);
                    }

                    this.OnGetChildElementsXml(element, writer);

                    writer.WriteEndElement();
                }
            }
        }

        /// <summary>
        /// Gets an <c>BuildElement</c> from XML.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="buildFileDefinition">The build file definition.</param>
        /// <param name="errors">The errors.</param>
        /// <returns></returns>
        public override IBuildElement GetBuildElement(XPathNavigator node, BuildFileDefinition buildFileDefinition, List<TransformationError> errors) {
            ProjectBuildElement element = buildFileDefinition.CreateNewBuildElement(typeof(ProjectBuildElement)) as ProjectBuildElement;

            element.XmlNamespace = node.NamespaceURI;

            if (string.IsNullOrEmpty(node.NamespaceURI)) {
                errors.Add(new TransformationError("Xml Namespace is missing!", element));
            }

            if (node.MoveToFirstAttribute()) {
                do {
                    if (node.Name.Equals(this.GetBuildElementPropertyName("InitialTargets"))) {
                        TargetCollection initialTargets = BuildElementPropertyTransformationHelper.ListOfTargetsFromXml(node.Value, buildFileDefinition);
                        if (initialTargets != null) {
                            element.InitialTargets.AddRange(initialTargets);
                        }
                    }
                    else if (node.Name.Equals(this.GetBuildElementPropertyName("DefaultTargets"))) {
                        TargetCollection defaultTargets = BuildElementPropertyTransformationHelper.ListOfTargetsFromXml(node.Value, buildFileDefinition);
                        if (defaultTargets != null) {
                            element.DefaultTargets.AddRange(defaultTargets);
                        }
                    }
                } while (node.MoveToNextAttribute());
            }
            return element;
        }
    }
}
